-- UIPetShopGoodsDetail
-- Create by cheny3 Nov/22/2015
-- 碎片兑换商品详细信息界面

-- UIPetShopGoodsDetail继承自Layer
UIPetShopGoodsDetail = class("UIPetShopGoodsDetail", function()
    return cc.Layer:create();
end);

function UIPetShopGoodsDetail.create(goodsIndex)
    return UIPetShopGoodsDetail.new(goodsIndex);
end

-- 构造函数
function UIPetShopGoodsDetail:ctor(goodsIndex)
    self:setName("UIPetShopGoodsDetail");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/shop/BlackMarketGoodsDetail.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录商品信息
    self.goodsIndex = goodsIndex;
    local goodsInfo = PetShopM.getGoodInfoInShop(self.goodsIndex);
    self.goodsInfo = goodsInfo;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件处理回调函数
function UIPetShopGoodsDetail:registerEventCallback()

end

-- 注册点击事件
function UIPetShopGoodsDetail:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIPetShopGoodsDetail");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确认购买按钮点击事件
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    local function onBuyClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 尝试购买
            self:tryBuy();
        end
    end
    btnBuy:addTouchEventListener(onBuyClick);
    TextStyleM.setTextStyle(btnBuy, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBuy:setTitleText(getLocStr("btn_text_confirm_exchange"));
end

-- 重绘
function UIPetShopGoodsDetail:redraw()
    -- 显示商品名称
    local CT = findChildByName(self.node, "CT");
    local nameLabel = CT:getChildByName("name");
    local classId = self.goodsInfo["rare_pet_chips"];
    local goodNum = self.goodsInfo["rare_chips_num"];
    local itemInfo = ItemM.query(classId);

    local nameDesc;
    if goodNum > 1 then
        nameDesc = string.format(getLocStr("item_bonus_format"), itemInfo["name"], goodNum);
    else
        nameDesc = itemInfo["name"];
    end
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(nameDesc);

    -- 显示商品描述
    local desc = ItemM.getItemDesc(classId);
    local detail = ItemM.getItemDetail(classId);
    -- 需求此处不显示细节描述了
    detail = "";

    local descLabel = CT:getChildByName("desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    local detailLabel = CT:getChildByName("detail");
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    detail = "[blue]" .. detail .. "[-]";
    detailLabel = applyString(detailLabel, detail);
    detailLabel:setVisible(false);

    -- 显示商品价格
    local costName = ItemM.query(self.goodsInfo["dungeon_pet_chips"], "name");
    local num = self.goodsInfo["dungeon_chips_cost"];
    local ownAmount = ItemM.getAmount(ME.user, self.goodsInfo["dungeon_pet_chips"]);
    local itemDesc = string.format(getLocStr("attrib_value_format"), ownAmount, num);
    if num > ownAmount then
        itemDesc = "[red]" .. itemDesc .. "[-]";
    end
    local costDesc = string.format(getLocStr("change_cost_scraps2"), costName, itemDesc);
    costDesc = costDesc .. "\n";

    -- 显示拥有商品数量
    local amountLabel = CT:getChildByName("own_amount");
    TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GREEN);
    local amount = ItemM.getOwnedAmount(ME.user, classId);
    costDesc = costDesc .. string.format(getLocStr("own_goods_amount2"), amount);
    amountLabel = applyString(amountLabel, costDesc);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { nameLabel, descLabel, detailLabel, amountLabel, btnBuy});

    -- 显示商品阶位
    local rank = ItemM.query(classId, "rank") or 0;
    if rank > 0 then
        local starNode = findChildByName(self.node, "CT/star_node");
        local layer = createStarLayer(rank, "images/ui/symbol/star_large.png", "right");
        if layer then
            starNode:removeAllChildren();
            starNode:addChild(layer);
        end
        starNode:setPositionY(nameLabel:getPositionY());
    end

    -- 按钮置灰判断
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    if PetShopM.canChangeScraps(self.goodsIndex) ~= true then
        btnBuy:setBright(false);
        btnBuy:setTouchEnabled(false);
    end
end

-- 适配
function UIPetShopGoodsDetail:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 尝试购买
function UIPetShopGoodsDetail:tryBuy()
    -- 判断能否兑换
    local ret = PetShopM.canChangeScraps(self.goodsIndex);
    if ret ~= true then
        AudioM.playFx("market_buy_failed");
        if ret == "sold_out" then
            -- 商品已售完
            alert(getLocStr("goods_sold_out"));
        elseif string.endWith(ret, "_lack") then
            -- 属性不足
            local field = string.sub(ret, 1, -6);
            if field == "gem" then
                showGemLackConfirm();
            else
                local costName = ItemM.query(self.goodsInfo["dungeon_pet_chips"], "name");
                local msg = string.format(getLocStr("fail_to_do_operate"), costName, getLocStr("exchange"));
                alert(msg);
            end
        else
            -- 未知原因
            cclog("购买商品失败，原因：" .. ret );
        end
        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIPetShopGoodsDetail");
        return;
    end

    -- 执行购买
    AudioM.playFx("market_buy_success");
    Operation.cmd_pet_shop_exchange_scraps(self.goodsIndex);

    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIPetShopGoodsDetail");
end

